﻿using Model;
using System;
using System.Data;

namespace BLL
{
    public class Qs_FileBLL : BaseBLL
    {
        public int INSERT(Qs_File model, string operName)
        {
            model.N_file_id = GetSequence("SEQ_FILE_ID");
            string sql = string.Format(@"insert into qs_file
  (n_file_id,
   n_business_id,
   c_file_name,
   c_file_path,
   c_file_operator,
   D_file_operator_time,
   c_remark) VALUES({0},{1},'{2}','{3}','{4}',sysdate,'{5}')", model.N_file_id, model.N_business_id, model.C_file_name, model.C_file_path, model.C_file_operator, model.C_remark);

            int count = base.NoQuery(sql, operName);

            return count;
        }

        public int DELETE(decimal fileId, string operName)
        {
            string sql = string.Format(@"DELETE  Qs_File WHERE Qs_File.N_file_id=" + fileId + "");
            int count = base.NoQuery(sql, operName);
            return count;
        }

        public DataTable GetFile(decimal business)
        {
            string sql = string.Format(@"SELECT
	f.N_file_id fileId,
	b.N_Business_Name busName,
	f.C_file_name fileName,
	f.C_file_path filePath,
	f.C_file_operator operatorId,
	f.D_file_operator_Time operatorDate
FROM
	QS_Business b,
	Qs_File f
WHERE
	b.N_business_id = f.N_business_id
    AND f.N_business_id=" + business + "");

            DataTable dt = base.Query(sql, "", "", "", "");
            return dt;
        }

        public Qs_File GetFileById(decimal fileId)
        {
            string sql = string.Format(@"SELECT * FROM Qs_File WHERE N_file_id=" + fileId + "");

            DataTable dt = base.Query(sql, "", "", "", "");
            Qs_File model = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                model = new Qs_File();
                foreach (DataRow dr in dt.Rows)
                {
                    model.N_file_id = decimal.Parse(dr["N_file_id"].ToString());
                    model.N_business_id = decimal.Parse(dr["N_business_id"].ToString());
                    model.C_file_name = dr["C_file_name"].ToString();
                    model.C_file_path = dr["C_file_path"].ToString();
                    model.C_file_operator = dr["C_file_operator"].ToString();
                    model.D_file_operator_Time = Convert.ToDateTime(dr["D_file_operator_Time"].ToString());
                    model.C_remark = dr["C_remark"].ToString();
                }
            }
            return model;
        }
    }
}